package com.kg.fiber.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ivan.toolkit.common.Events;
import com.kg.fiber.domain.NetNode;

import lombok.Getter;
import lombok.extern.log4j.Log4j;

@Component
@Log4j
public class Warnings {
	
	private Map<String, Item> warningLock = Maps.newHashMap();
	
	@Getter
	private List<Item> warnings = Lists.newLinkedList();
	
	public void feed(NetNode node) {
		
		Item lock = warningLock.get(node.getHost());
		if(lock != null) {
			log.info(node.getHost() + " is warning locked, ignore");
			return;
		}
		
		Item item = new Item();
		item.time = System.currentTimeMillis();
		item.host = node.getHost();
		item.mark = node.getMark();
		
		warningLock.put(node.getHost(), item);
		warnings.add(item);
		
		Events event = new Events("WARNING_HAPPEN");
		event.put(item);
		Events.post(event);
		
	}
	
	public void confirm(String host) {
		
		Item lock = warningLock.get(host);
		if(lock != null) {
			warningLock.remove(host);
			warnings.remove(lock);
			
			Events event = new Events("WARNING_CONFIRM");
			event.put(lock);
			Events.post(event);
		}
		
	}
	
	
	public static class Item {
		
		@Getter
		private long time;
		
		@Getter
		private String host;
		
		@Getter
		private String mark;
		
	}

}
